﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using Interfaces.General_Interfaces;
using Interfaces.Add_Interfaces;
using Interfaces.Information_Interfaces;
using Repository;
using Presenters.Managers;
using Presenters.Inherited_Presenters;
using Interfaces.Delete_Interfaces;
using Interfaces.Update_Interfaces;
using Model.Filters;
using Model.Query_Model;
using System.Linq;

namespace Presenters
{
    public class RoadsViewPresenter:OperationStatus
    {
        public int idRoad = 0;
        private IList<RoadOutput> roadList = null;
        private RoadOutput selectedRoad = null;
        private RoadManager manager = new RoadManager();
        private IRoadsView view = null;

        public RoadsViewPresenter(IRoadsView view)
        {
            this.view = view;
        }

        /// <summary>
        /// Otwiera okno umożliwiające usunięcie zaznaczonej wyceny (wraz z historią zmian)
        /// </summary>
        /// <param name="view">Implementacja IRoadDeleteView</param>
        public void RoadDeleteDisplay(IRoadDeleteView view)
        {
            view.SetParams(selectedRoad);
            view.ReturnView<Form>().ShowDialog();
        }

        /// <summary>
        /// Otwiera okno umożliwiające dodanie do bazy danych nowego obiektu typu Road, Valuation (Wyceny)
        /// </summary>
        /// <param name="view">Implementacja IRoadAddView</param>
        public void RoadAddDisplay(IRoadAddView view)
        {
            view.ReturnView<Form>().ShowDialog();
        }

        public void RoadUpdateDisplay(IRoadUpdateView view)
        {
            view.SetParams(selectedRoad);
            view.ReturnView<Form>().ShowDialog();
        }

        public void SelectRoad(int index)
        {
            selectedRoad = roadList.Where(x => x.idRoad == index).FirstOrDefault<RoadOutput>();
        }

        /// <summary>
        /// Otwiera okno umożliwiające aktualizację wyceny zaznaczonej trasy
        /// </summary>
        /// <param name="view">Implementacja IValuationUpdateView</param>
        public void ValuationUpdateDisplay(IValuationUpdateView view)
        {
            if (selectedRoad != null)
            {
                view.SetParams(selectedRoad);
                view.ReturnView<Form>().ShowDialog();
            }
        }

        /// <summary>
        /// Wyświetla wszystkie relacje tras, wraz z wycenami
        /// </summary>
        public void GetAllRoads()
        {
            nameOperation = "Pobieranie wycen";
            try
            {
                roadList = manager.RoadSearch(new RoadCriteria { });
                view.roadList = roadList;
            }
            catch (Exception ex)
            {
                exception = ex.Message;
                view.ShowError();
            }
        }

        public void DocumentAdd(IDocumentAddView view)
        {
            view.ReturnView<Form>().ShowDialog();
        }
    }
}
